﻿using System;
using System.Collections.Generic;
using System.Text;
using EmbeddedSharp;
using EmbeddedSharp.Core;
using EmbeddedSharp.Core.Function;

namespace EmbeddedSharp.Scripting.Function.IO {

    /// <summary>
    /// Delete either file or folder.
    /// 
    /// The parameter is one or more file or folder path.
    /// </summary>
    public class DeleteNode : FuncNode {

        public DeleteNode()
            : base() {
            base.Value = "delete";
        }

        public override object Evaluate() {
            if (Parameters.Count < 1) {
                return "usage: delete(file1, file2, file3, ...)";
            }

            StringBuilder buff = new StringBuilder();

            foreach (Node child in Parameters) {
                string name = (string)child.Evaluate();
                if (System.IO.Directory.Exists(name)) {
                    System.IO.Directory.Delete(name);
                } else if (System.IO.File.Exists(name)) {
                    System.IO.File.Delete(name);
                }
            }

            return buff.ToString();
        }
    }
}
